package com.hlhj.carview.aty

import android.app.Activity
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.ItemsAskView
import com.hlhj.carview.R
import com.hlhj.carview.Toast
import com.hlhj.carview.adapter.ItemAskListAdp
import com.hlhj.carview.bean.ItemAskListBean
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.customView.IteamAskDialog
import com.hlhj.carview.http.Apis
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.ItemAskPresenter
import com.hlhj.carview.utils.LoadingUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_itemask.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 *NeverMore
 *2020/9/7
 **/
class ItemAskAty:BaseAty<ItemsAskView,ItemAskPresenter>(), ItemsAskView {
    private lateinit var adp:ItemAskListAdp
    private val datas=ArrayList<ItemAskListBean.Data>()

    override fun initView() {
        tvTittle.text="委托列表"
        adp= ItemAskListAdp(datas)
        listView.adapter=adp

    }

    override fun logicStart() {
        val httpParams = HttpParams()
        httpParams.put("uriPath","itemAsk")
        httpParams.put("childType",intent.getStringExtra("name"))
        OkGo.post<ItemAskListBean>(Urls.PubUrl)
                .headers("type",3.toString())
                .params(httpParams)
                .converter(object : MyJsonConvert<ItemAskListBean>(){})
                .adapt(ObservableResponse<ItemAskListBean>())
                .subscribeOn(Schedulers.io())
                .subscribe(object :CustomAction<Response<ItemAskListBean>>(this){
                    override fun httpSuccess(t: Response<ItemAskListBean>) {
                        t.body().data?.let { it1 -> datas.addAll(it1) }
                        adp.notifyDataSetChanged()
                    }
                })

    }

    override fun setListener() {
        adp.setOnItemClickListener { adapter, view, position ->
            val dialog=IteamAskDialog(this)
            dialog.callback=object : () -> Unit {
                override fun invoke() {
                    LoadingUtil.show(this@ItemAskAty)
                    OkGo.post<MyBaseBean>(Urls.PubUrl)
                            .headers("type",1.toString())
                            .params("uriPath","itemAskList")
                            .params("level",datas[position].level)
                            .params("loc",datas[position].loc)
                            .params("long",datas[position].long)
                            .params("name",datas[position].name)
                            .converter(object : MyJsonConvert<MyBaseBean>(){})
                            .adapt(ObservableResponse<MyBaseBean>())
                            .subscribeOn(Schedulers.io())
                            .subscribe(object :CustomAction<Response<MyBaseBean>>(this@ItemAskAty){
                                override fun httpSuccess(t: Response<MyBaseBean>) {
                                    runOnUiThread {
                                        LoadingUtil.diss()
                                        "申请成功，委托商将会在24小时候内联系您".Toast()
                                    }
                                }
                            })

                }

            }
                    dialog.show("确认申请配送委托","申请成功，委托商将会在24小时候内联系您")
        }
    }

    override fun getContentID(): Int {
        return R.layout.aty_itemask
    }

    override fun bindView(): ItemsAskView {
        return this
    }

    override fun createPresenter(): ItemAskPresenter {
        return ItemAskPresenter()
    }
}